package com.sm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.controller.request.SelectCourseRequest;
import com.sm.controller.request.SelectCourseStudentRequest;
import com.sm.controller.respond.SelectCourseAchievementRespond;
import com.sm.controller.respond.SelectCourseStudentRespond;
import com.sm.dao.CourseMapper;
import com.sm.dao.StudentMapper;
import com.sm.dao.TeacherMapper;
import com.sm.dao.UserMapper;
import com.sm.entity.Course;
import com.sm.entity.Student;
import com.sm.entity.Teacher;
import com.sm.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: lpf
 * @Date: 2022/2/15
 */

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired(required = false)
    CourseMapper courseMapper;

    @Autowired(required = false)
    StudentMapper studentMapper;

    @Autowired(required = false)
    TeacherMapper teacherMapper;

    @Autowired(required = false)
    UserMapper userMapper;

    @Override
    public PageInfo<Course> getAllCourse(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(courseMapper.getCourseListForSelect());
    }

    /**
     * @Description: 获得某个课程的学生成绩（分页）
     * @Author: lpf
     * @Date: 2022/2/16
     */
    @Override
    public List<SelectCourseAchievementRespond> getStudentAchievement(SelectCourseStudentRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());
        return studentMapper.getCourseAchievement(request.getCourseId());
    }

    /**
     * @Description: 获得某个课程的学生成绩（不分页）
     * @Author: lpf
     * @Date: 2022/3/6
     */
    @Override
    public List<SelectCourseAchievementRespond> getStudentAchievement(Integer courseId) {
        return studentMapper.getCourseAchievement(courseId);
    }

    /**
     * @Description: 获得选的某个课程的所有学生
     * @Author: lpf
     * @Date: 2022/2/16
     */
    @Override
    public List<SelectCourseStudentRespond> getCourseAttender(SelectCourseRequest request) {
        return studentMapper.getCourseAttender(request.getCourseId());
    }

    @Override
    public List<Student> getAllStudent() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        return studentMapper.selectList(queryWrapper);
    }

    /**
     * @Description: 修改某个课程信息
     * @Author: lpf
     * @Date: 2022/3/6
     */
    @Override
    public void updateCourseList(Course course) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", course.getTeacherName());
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        course.setTeacherId(teacher.getId());
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", course.getId());
        course.setTeacherName(null);
        courseMapper.update(course, updateWrapper);
    }

    /**
     * @Description: 强制让某个学生从某个课程退选
     * @Author: lpf
     * @Date: 2022/3/5
     */
    @Override
    public void deleteCourseStudent(Integer courseId, Integer studentId) {
        teacherMapper.deleteCourseStudent(courseId, studentId);
    }

    /**
     * @Description: 修改教师自身信息
     * @Author: lpf
     * @Date: 2022/3/5
     */
    @Override
    public void updateTeacher(Teacher teacher) {
        UpdateWrapper<Teacher> teacherUpdateWrapper = new UpdateWrapper<>();
        teacherUpdateWrapper.eq("username", teacher.getUsername());
        teacherMapper.update(teacher, teacherUpdateWrapper);
    }

    /**
     * @Description:
     * @Author: lpf
     * @Date: 2022/3/6
     */
    @Override
    public Integer selectStudentByName(String studentName) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("name", studentName);
        Student student = studentMapper.selectOne(studentQueryWrapper);
        return student.getUserId();
    }

    /**
     * @Description: 修改学生成绩
     * @Author: lpf
     * @Date: 2022/3/6
     */
    @Override
    public void updateStudentScore(Integer score, Integer courseId, Integer studentId) {
        teacherMapper.updateStudentScore(score, courseId, studentId);
    }

    /**
     * @Description: 注销
     * @Author: lpf
     * @Date: `2022/3/8`
     */
    @Override
    public void deleteTeacherAndUser(Integer id) {
        teacherMapper.deleteById(id);
        userMapper.deleteById(id);
    }
}

